package com.yyk.yykywd.ui.customer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.config.HttpUrl;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.ClentClientdetailInfo;
import com.yyk.yykywd.ui.WebActivity;
import com.yyk.yykywd.ui.work.QueryWorkApplyActivity;
import com.yyk.yykywd.ui.work.QueryWorkPactActivity;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;

import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 信息展示
 */
public class MyCustomerActivity extends BaseActivity {


    @BindView(R.id.tv_hname) //机构名称
            TextView tvHname;
    @BindView(R.id.tv_state)//访问状态
            TextView tvState;
    @BindView(R.id.tv_num) //跟进次数
            TextView tvNum;
    @BindView(R.id.tv_createtime) //上传时间
            TextView tvCreatetime;
    @BindView(R.id.tv_name)// 拜访人姓名
            TextView tvName;
    @BindView(R.id.tv_postname) //拜访人职称
            TextView tvPostname;
    @BindView(R.id.tv_phone)//拜访人手机号
            TextView tvPhone;
    @BindView(R.id.tv_defeated) //原因描述
            TextView tvDefeated;
    @BindView(R.id.ll_xmsqhzs) //项目申请书
            LinearLayout llXmsqhzs;
    @BindView(R.id.ll_ckht)//查看合同
            LinearLayout llCkht;
    @BindView(R.id.ll_ckhk) //查看回款
            LinearLayout llCkhk;
    @BindView(R.id.ll_ywgzlc) //业务跟踪流程
            LinearLayout llYwgzlc;
    @BindView(R.id.ll_center_4) //中间4个隐藏
            LinearLayout llCenter4;
    @BindView(R.id.ll_bottom_4) //底部4个隐藏
            LinearLayout llBottom4;
    @BindView(R.id.ll_defeated) //原因描述
            LinearLayout llDefeated;
    @BindView(R.id.ll_bottom_3) //
            LinearLayout llBottom3;
    @BindView(R.id.iv_img) //业务负责人头像
            ImageView ivImg;
    @BindView(R.id.tv_unmae) //业务负责人名字
            TextView tvUnmae;
    @BindView(R.id.ll_ywfzr) //业务负责人显示
            LinearLayout llYwfzr;
    @BindView(R.id.tv_delyuanyin) //计划已删除改变原因提示
            TextView tvDelyuanyin;
    @BindView(R.id.ll_center_3)
    LinearLayout llCenter3;
    private String id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_customer;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("我的客户");
    }

    @Override
    public void initData() {
        initGetIntent();
        postClentClientdetail();
    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
        }

    }

    /**
     * 接口31:客户列表（whm）
     */
    private void postClentClientdetail() {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid") + "");
        map.put("bid", id + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postClentClientdetail(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Observer<ClentClientdetailInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ClentClientdetailInfo info) {
                        Logger.e("32:机构详情" + info.toString());
                        if (info.getCode() == 1) {
                            //2 是计划删除页面
                            ClentClientdetailInfo.DataBean data = info.getData();
                            tvHname.setText(data.getHname()); //必须写
                            tvNum.setText("跟进次数：" + data.getNum() + "次");
                            tvCreatetime.setText(data.getCreatetime());
                            tvName.setText(data.getName());
                            tvDefeated.setText(data.getDefeated());
                            tvPhone.setText(data.getPhone());
                            tvPostname.setText(data.getPost_name());

                            if (data.getDel() == 2) {
                                tvState.setText("计划已删除");
                                tvState.setTextColor(getResources().getColor(R.color.gray_b3));
                                llYwfzr.setVisibility(View.VISIBLE);
                                llCenter3.setVisibility(View.GONE);
                                llBottom4.setVisibility(View.GONE);
                                tvDelyuanyin.setText("相关描述:");
                                tvDefeated.setText(data.getDel_remark());
                                tvUnmae.setText(data.getUname());
                                Glide.with(mActivity).load(HttpUrl.BASE_PIC_URL + data.getImg()).into(ivImg);

                            } else {
                                int state = data.getState(); //访问状态
                                if (state == 1) {
                                    tvState.setText("未拜访");
                                    tvNum.setVisibility(View.GONE);
                                    llCenter4.setVisibility(View.GONE);
                                    llBottom4.setVisibility(View.GONE);
                                } else if (state == 2) {
                                    tvState.setText("未拜访");
                                    tvNum.setVisibility(View.GONE);
                                    llCenter4.setVisibility(View.GONE);
                                    llBottom4.setVisibility(View.GONE);
                                } else if (state == 3) {
                                    tvState.setText("待签合同");
                                    llDefeated.setVisibility(View.GONE);
                                    llCkhk.setVisibility(View.GONE);
                                    llCkht.setVisibility(View.GONE);

                                } else if (state == 4) {
                                    tvState.setText("待回款");
                                    llDefeated.setVisibility(View.GONE);
                                    llCkhk.setVisibility(View.GONE);
                                } else if (state == 5) {
                                    tvState.setText("合作成功");
                                    tvState.setTextColor(getResources().getColor(R.color.green_2a));
                                    llDefeated.setVisibility(View.GONE);
                                } else if (state == 6) {
                                    tvState.setText("仍需跟进");
                                    llDefeated.setVisibility(View.GONE);
                                    llBottom3.setVisibility(View.GONE);
                                } else if (state == 7) {
                                    tvState.setText("合作未达成");

                                    llBottom3.setVisibility(View.GONE);

                                }

                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.t("异常" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 1.查看项目申请书
     * 2.查看合同
     * 3.查看回款
     * 4.业务流程跟踪
     *
     * @param view
     */
    @OnClick({R.id.ll_xmsqhzs, R.id.ll_ckht, R.id.ll_ckhk, R.id.ll_ywgzlc})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_xmsqhzs:
                Bundle bundle = new Bundle();
                bundle.putString("id", id + "");
                startAct(QueryWorkApplyActivity.class, bundle);
                break;
            case R.id.ll_ckht:
                Bundle bundle2 = new Bundle();
                bundle2.putString("id", id + "");
                startAct(QueryWorkPactActivity.class, bundle2);
                break;
            case R.id.ll_ckhk:
                Bundle bundle3 = new Bundle();
                bundle3.putString("id", id + "");
                startAct(QueryHuikuanActivity.class, bundle3);
                break;
            case R.id.ll_ywgzlc:
                Intent intent = new Intent();
                //业务跟踪流程
                intent.putExtra("url", HttpUrl.BASE_H5_URL + "business/?uid=" + Hawk.get("uid") + "&did=" + id);
                startAct(intent, WebActivity.class, null);
                break;
        }
    }


}
